﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace D2DEngine.Tile
{
    public class D2DTileLayer
    {
        protected float alpha = 1.0f;
        protected int[,] layout;

        protected float depth = 1.0f;

        /// <summary>
        /// The Depth of the layer
        /// </summary>
        public float Depth
        {
            get { return this.depth; }
            set { this.depth = value; }
        }

        /// <summary>
        /// The alpha channel of the layer
        /// </summary>
        public float Alpha
        {
            get { return this.alpha; }
            set { this.alpha = value; }
        }

        /// <summary>
        /// The layout of the layer.
        /// </summary>
        public int[,] Layout
        {
            get { return this.layout; }
            set { this.layout = value; }
        }

        /// <summary>
        /// An integer representing the number
        /// of cell in one row of the layer.
        /// </summary>
        public int Width
        {
            get { return layout.GetLength(1); }
        }

        /// <summary>
        /// An integer representing the number
        /// of cell in one column of the layer.
        /// </summary>
        public int Height
        {
            get { return layout.GetLength(0); }
        }

        public D2DTileLayer(int width, int height)
        {
            layout = new int[height, width];
        }


        /// <summary>
        /// Sets an index to the cell corresponding
        /// to the x,y value given in parameters
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="index"></param>
        public void SetCellIndex(int x, int y, int index)
        {
            this.layout[y, x] = index;
        }


        /// <summary>
        /// Draws the layer
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="tileSet"></param>
        public void Draw(SpriteBatch spriteBatch, D2DTileSet tileSet)
        {
            Rectangle sourceRect;
            Rectangle destinationRect;

            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    if (this.layout[y, x] == -1)
                        continue;

                    destinationRect = new Rectangle(
                            x * tileSet.TileSize,
                            y * tileSet.TileSize,
                            tileSet.TileSize,
                            tileSet.TileSize
                        );

                    sourceRect = tileSet.GetSourceRectangle(this.layout[y, x]);

                    spriteBatch.Draw(tileSet.Texture,
                        destinationRect,
                        sourceRect,
                        new Color(new Vector4(1f, 1f, 1f, this.alpha)));
                }
            }
        }

            /// <summary>
        /// Draws the layer
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="tileSet"></param>
        public void Draw(SpriteBatch spriteBatch, D2DTileSet tileSet, Point min, Point max)
        {
            Rectangle sourceRect;
            Rectangle destinationRect;

            min.X = Math.Max(min.X, 0);
            min.Y = Math.Max(min.Y, 0);

            max.X = Math.Min(max.X, Width);
            max.Y = Math.Min(max.Y, Height);

            for (int x = min.X; x < max.X; x++)
            {
                for (int y = min.Y; y < max.Y; y++)
                {
                    if (this.layout[y, x] == -1)
                        continue;

                    destinationRect = new Rectangle(
                            x * tileSet.TileSize,
                            y * tileSet.TileSize,
                            tileSet.TileSize,
                            tileSet.TileSize
                        );

                    sourceRect = tileSet.GetSourceRectangle(this.layout[y, x]);

                    spriteBatch.Draw(
                        tileSet.Texture,
                        destinationRect,
                        sourceRect,
                        new Color(new Vector4(1f, 1f, 1f, this.alpha)),
                        0.0f,
                        Vector2.Zero,
                        SpriteEffects.None,
                        this.depth / 10);                    
                }
            }
        }

        
    }
}
